app.controller('BusShiftTplAttachCtrl', function ($scope, $modal,$modalInstance, param, ajax, toaster) {

	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}
	$scope.startList = [];
	$scope.attached = {};
	$scope.attachedFlag = false;

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.param.data.stationFilter=null;//防止json转换出错 并未修改值

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.startList.selected == undefined) {
				toaster.pop('warning', '提示', '请选择汽车票始发客运站！');
				return;
			}
			$scope.attached.start = $scope.startList.selected;
			$scope.attached.template = $scope.param.data;

			var promise = ajax.call(new Req.GET({
				resource: 'shift/bs/'+$scope.param.data.id+'/list'
			}));
			promise.then(function (result) {
				$scope.shiftList = result.result;

				if($scope.shiftList.length>0){
					var modalInstance = $modal.open({
						templateUrl: 'template/bus/shiftTemplate/attachShiftList.html?v=38a428a1c4',
						controller: 'AttachShiftUpdateCtrl',
						size: 'lg',
						resolve: {
							param: function() {
								return {
									"list": $scope.shiftList
								};
							}
						}
					});
					modalInstance.result.then(function(param) {
						if(param.length>0){
							inserter.datalist = param;
						}
						inserter.fields = $scope.attached;
						inserter.resource = 'shift/bs/attach/bk';
						ajax.call(inserter).then(function (result) {
							if (result.code == 200) {
								toaster.pop('success', '成功', '已关联汽车票班次。');
								$modalInstance.close();
							} else {
								toaster.pop('error', '错误', result.result);
							}
						}, function () {
							toaster.pop('error', '错误', "连接错误，无法连接到服务器！");
						});
					});
				}else{
					inserter.fields = $scope.attached;
					inserter.resource = 'shift/bs/attach/bk';
					ajax.call(inserter).then(function (result) {
						if (result.code == 200) {
							toaster.pop('success', '成功', '已关联汽车票班次。');
							$modalInstance.close();
						} else {
							toaster.pop('error', '错误', result.result);
						}
					}, function () {
						toaster.pop('error', '错误', "连接错误，无法连接到服务器！");
					});
				}
			}, function () { });
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};

	$scope.detach = function () {
		if (!$scope.attachedFlag) return;
		var promise = ajax.call(new Req.GET({
			resource: 'shift/bs/'+$scope.param.data.id+'/list'
		}));
		promise.then(function (result) {
			$scope.shiftList = result.result;
			if($scope.shiftList.length>0){
				var modalInstance = $modal.open({
					templateUrl: 'template/bus/shiftTemplate/attachShiftList.html?v=38a428a1c4',
					controller: 'AttachShiftUpdateCtrl',
					size: 'lg',
					resolve: {
						param: function() {
							return {
								"list": $scope.shiftList
							};
						}
					}
				});
				modalInstance.result.then(function(param) {
					if(param.length>0){
						inserter.datalist = param;
						console.log(param);
					}
					inserter.fields = $scope.param.data;
					inserter.resource = 'shift/bs/detach/bk/cancel';
					ajax.call(inserter).then(function (result) {
						if (result.code == 200) {
							toaster.pop('success', '成功', '已取消班次共享。');
							$modalInstance.close();
						} else {
							toaster.pop('error', '错误', result.result);
						}
					}, function () {
						toaster.pop('error', '错误', "连接错误，无法连接到服务器！");
					});
				});
			}else{
				inserter.fields = $scope.param.data;
				inserter.resource = 'shift/bs/detach/bk/cancel';
				ajax.call(inserter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '已取消班次共享。');
						$modalInstance.close();
					} else {
						toaster.pop('error', '错误', result.result);
					}
				}, function () {
					toaster.pop('error', '错误', "连接错误，无法连接到服务器！");
				});
			}
		}, function () { });
		//ajax.call(new Req.POST({
		//	resource: 'shift/bs/detach/bk/' + $scope.param.data.id
		//})).then(function (result) {
		//	if (result.code == 200) {
		//		toaster.pop('success', '成功', '已取消班次共享。');
		//		$modalInstance.close();
		//	} else {
		//		toaster.pop('error', '错误', result.result);
		//	}
		//}, function () {
		//	toaster.pop('error', '错误', "连接错误，无法连接到服务器！");
		//});
	};

	//加载关联关系
	ajax.get('shift/bs/attach/bk/' + $scope.param.data.id, function (result) {
		if (result && result.code == 200) {
			$scope.attached = result.result;
			$scope.attachedFlag = true;
			if ($scope.startList.length > 0) $scope.startList.selected = $scope.attached.start;
			else $scope.sel = $scope.attached.start;
		}
	}, function () { });

	//起始客运站列表读取
	ajax.get('starting/bk/list', function (result) {
		if (result && result.code == 200) {
			$scope.startList = result.result;
			if ($scope.sel) $scope.startList.selected = $scope.sel;
		}
	}, function () { });
});